/*
 * Code based on FIR/IIR documentation found at:
 * http://www-svr.eng.cam.ac.uk/~ajr/SpeechAnalysis/node13.html 
 */

#define FILTER_LENGTH 256
#define SIGNAL_LENGTH 1024

float input_data[SIGNAL_LENGTH];
float output_data[SIGNAL_LENGTH];
float filter1[FILTER_LENGTH];
float filter2[FILTER_LENGTH];

/*
 *
 * Note that the filters are backwards.
 *
 */

void applyIIR(	float input[SIGNAL_LENGTH],

		float inFilter[FILTER_LENGTH],

		float outFilter[FILTER_LENGTH],

		float output[FILTER_LENGTH]) {

	int i,j;
	//printf("Before Loop1\n");
	for (i=0; i<FILTER_LENGTH; i++) {
		//printf("Loop1: i = %d\n", i);
		output[i] = 0.0;
		for (j=FILTER_LENGTH-i-1; j<=FILTER_LENGTH-1; j++) {
			
			//printf("Loop1: i,j = %d,%d\n", i, j);
			output[i] = output[i] +
				input[j-FILTER_LENGTH+1+i]*inFilter[j] +
				output[j-FILTER_LENGTH+1+i]*outFilter[j];
		}
	}
	//printf("Before Loop2\n");
	for (i=FILTER_LENGTH; i<SIGNAL_LENGTH; i++) {
		//printf("Loop2: i = %d\n", i);
		output[i]=0.0;
		for (j=0; j<FILTER_LENGTH; j++) {
			//printf("Loop2: i,j = %d,%d\n", i, j);
			output[i] = output[i] +
				input[j-FILTER_LENGTH+1+i]*inFilter[j] +
				output[j-FILTER_LENGTH+1+i]*outFilter[j];
		}
	}
}
int main() {
	int i;
	filter2[0] = 0.0; /* Must be zero */
	for (i=0; i<300; i++) {
		applyIIR(input_data, filter1, filter2, output_data);
	}
	return 0;
}
